package cn.danger.com.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.danger.com.model.entity.Memo;

/**
 * Created by 俊 on 2016/12/30.
 */
public class MemoDaoImpl implements MemoDao {

    private DBHelper helper;
    private static final String SQL_INSERT = "insert into memo(objectId,username,title,date,content) values(?,?,?,?,?)";
    private static final String SQL_DELETE = "delete from memo where id = ?";
    private static final String SQL_DELETE_BY_USERNAME = "delete from memo where username = ?";
    private static final String SQL_UPDATE = "update memo set username = ?,objectId = ?,title = ?,date = ?,content = ? where id = ?";
    private static final String SQL_QUERY = "select * from memo where username = ?";
    private static final String SQL_QUERY_MEMO = "select * from memo where id = ?";
    private static final String SQL_QUERY_ID = "select max(id) from memo";

    public MemoDaoImpl(Context context) {
        helper = DBHelper.getInstance(context);
    }

    @Override
    public int insert(Memo memo) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(SQL_INSERT, new String[]{memo.getObjectId(), memo.getUsername(), memo.getTitle(),
                memo.getDate(), memo.getContent()});
        Cursor cursor = db.rawQuery(SQL_QUERY_ID, null);
        int id = -1;
        if (cursor.moveToNext()) {
            id = cursor.getInt(0);
            Log.i("danger", "id---->>" + id);
        }

        cursor.close();
        db.close();
        return id;
    }

    @Override
    public void delete(int id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(SQL_DELETE, new Object[]{id});
        db.close();
    }

    @Override
    public void delete(String username) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(SQL_DELETE_BY_USERNAME, new Object[]{username});
        db.close();
    }

    @Override
    public void update(Memo memo) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(SQL_UPDATE, new Object[]{memo.getUsername(), memo.getObjectId(),
                memo.getTitle(), memo.getDate(), memo.getContent(), memo.getId()});
        db.close();
    }

    @Override
    public Memo getMemo(int id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        Memo memo = null;
        Cursor cursor = db.rawQuery(SQL_QUERY_MEMO, new String[]{id + ""});
        if (cursor.moveToNext()) {
            memo = new Memo();
            memo.setId(id);
            memo.setObjectId(cursor.getString(cursor.getColumnIndex("objectId")));
            memo.setDate(cursor.getString(cursor.getColumnIndex("date")));
            memo.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            memo.setContent(cursor.getString(cursor.getColumnIndex("content")));
            memo.setTitle(cursor.getString(cursor.getColumnIndex("title")));
        }
        cursor.close();
        db.close();
        return memo;
    }

    @Override
    public List<Memo> getMemos(String username) {
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Memo> memos = new ArrayList<>();
        Cursor cursor = db.rawQuery(SQL_QUERY, new String[]{username});
        while (cursor.moveToNext()) {
            Memo memo = new Memo();
            memo.setId(cursor.getInt(cursor.getColumnIndex("id")));
            memo.setObjectId(cursor.getString(cursor.getColumnIndex("objectId")));
            memo.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            memo.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            memo.setDate(cursor.getString(cursor.getColumnIndex("date")));
            memo.setContent(cursor.getString(cursor.getColumnIndex("content")));
            memos.add(memo);
        }
        cursor.close();
        db.close();
        return memos;
    }
}
